package com.ruoyi.web.controller.biz;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

import com.ruoyi.biz.domain.WarnedRecord;
import com.ruoyi.common.enums.WarnedStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.biz.service.IWarnedRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2020-11-30
 */
@Api(tags = "报警")
@RestController
@RequestMapping("/system/warned/record")
public class WarnedRecordController extends BaseController
{
    @Autowired
    private IWarnedRecordService warnedRecordService;

    /**
     * 报警日期和报警次数
     * @param equipmentId
     * @return
     */
    @ApiOperation("测试图表的数据")
    @GetMapping("/timeData/count/{equipmentId}")
    public AjaxResult getTimeDataCount(@PathVariable Long equipmentId){
        Map<String,Integer> TimeDataCount =  new HashMap<>();//待封装数据集
        AtomicInteger atomicInteger = new AtomicInteger(1);
        WarnedRecord warnedRecord = new WarnedRecord();
        warnedRecord.setEquipmentId(equipmentId);
        List<WarnedRecord> warnedRecords = warnedRecordService.selectWarnedRecordList(warnedRecord);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        warnedRecords.forEach(item->{
            Date lastAlarmTime = item.getLastAlarmTime();
            String format = simpleDateFormat.format(lastAlarmTime);
            if (TimeDataCount.containsKey(format)){
                int incrementAndGet = atomicInteger.get();
                TimeDataCount.put(format,incrementAndGet);
                atomicInteger.incrementAndGet();
            }else {
                atomicInteger.set(1);
                TimeDataCount.put(format,atomicInteger.get());
                atomicInteger.incrementAndGet();
            }
        });
        return AjaxResult.success(TimeDataCount);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('operate:warn:record:list')")
    @GetMapping("/list")
    @ApiOperation("查询告警记录列表")
    public TableDataInfo list(WarnedRecord warnedRecord)
    {
        logger.info("查询告警记录列表对象:{}",warnedRecord);
        startPage();
        List<WarnedRecord> list = warnedRecordService.selectWarnedRecordList(warnedRecord);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('operate:warn:record:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出告警记录信息")
    public AjaxResult export(WarnedRecord warnedRecord)
    {
        List<WarnedRecord> list = warnedRecordService.selectWarnedRecordList(warnedRecord);
        ExcelUtil<WarnedRecord> util = new ExcelUtil<WarnedRecord>(WarnedRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('operate:warn:record:query')")
    @GetMapping(value = "/{warnedId}")
    @ApiOperation("获取告警记录信息")
    public AjaxResult getInfo(@PathVariable("warnedId") Long warnedId)
    {
        return AjaxResult.success(warnedRecordService.selectWarnedRecordById(warnedId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('operate:warn:record:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增告警记录信息")
    public AjaxResult add(@RequestBody WarnedRecord warnedRecord)
    {
        return toAjax(warnedRecordService.insertWarnedRecord(warnedRecord));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('operate:warn:record:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改告警记录信息")
    public AjaxResult edit(@RequestBody WarnedRecord warnedRecord)
    {
        return toAjax(warnedRecordService.updateWarnedRecord(warnedRecord));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('operate:warn:record:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{warnedIds}")
    @ApiOperation("删除告警记录信息")
    public AjaxResult remove(@PathVariable Long[] warnedIds)
    {
        return toAjax(warnedRecordService.deleteWarnedRecordByIds(warnedIds));
    }

    @PreAuthorize("@ss.hasPermi('operate:warn:record:list')")
    @GetMapping("/listAllWarnStatus")
    @ApiOperation("获取所有告警状态信息")
    public AjaxResult listAllWarnStatus() {
        WarnedStatus[] statuses = WarnedStatus.values();
        List<String> list = new ArrayList<>();
        for (WarnedStatus status : statuses) {
            list.add(status.getValue());
        }
        /*List<WarnedStatus> list = Arrays.stream(statuses).collect(Collectors.toList());*/
        return AjaxResult.success(list);
    }


}
